package pogvue.gui;

import java.util.*;
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

import javax.swing.*;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import pogvue.datamodel.*;
import pogvue.gui.event.*;
import pogvue.gui.hub.*;
import pogvue.analysis.*;
import pogvue.io.*;
import pogvue.util.*;

public final class TestViewport {

  public JScrollPane   jsp;
  public PogPanel      jc;
  public AlignmentI    al;
  public AlignViewport av;
  public int           bpp = 10;
  public Vector        pid;
  public RendererI cr = new ConsensusRenderer();

  public TestViewport(AlignmentI al) {

    this.al = al;

    av = new AlignViewport(1,al.getWidth(),1,al.getHeight(),false,false,false,false,Color.white);
    av.showGaps(false);
    av.setAlignment(al);

    jc  = new PogPanel();

    jc.setMinimumSize(new Dimension(al.getWidth()/bpp,650));
    jc.setPreferredSize(new Dimension(al.getWidth()/bpp,650));

    jsp = new JScrollPane(jc);
    
    JFrame jf = new JFrame();
    
    jf.setSize(700,700);

    jf.getContentPane().setLayout(new BorderLayout());
    jf.getContentPane().add("Center",jsp);

    jf.setVisible(true);

  }

  private class PogPanel extends JPanel {

    public void paint(Graphics g) {
      Rectangle rect  = g.getClip().getBounds();

      int x1 = (int)(rect.getX()*bpp);
      int x2 = x1 + (int)(rect.getWidth()*bpp) -1;
      int y1 = (int)(rect.getY()/10);
      int y2 = y1 + al.getHeight();

      g.setColor(Color.white);
      g.fillRect(0,0,getWidth(),getHeight());

      int i = 0;

      pid    = AAFrequency.calculate(al.getSequences(),x1,x2);

      while (i < al.getHeight()) {
	drawPanel(g,x1,x2,y1,y2,0,i,0);
	i++;
      }
      
    }

      
    public void drawPanel(Graphics g,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {
      double            charWidth  = (1.0/bpp);
      int               charHeight = 10;
    

      RendererI r  = cr;

      r.drawSequence(g,null,al.getSequenceAt(starty),
		   x1,
		   x2,
		   (int)((x1-startx)*charWidth),
		   offset + starty*charHeight,
		   charWidth,
		   charHeight,
		   false,false,false,
		   pid,
		   starty,
		   av,
		   new Hashtable(),
		   null);
    }
  }
  
  public static void main(String[] args) {

    AlignmentI al  = null;
    GFFFile    gff = null;
    SequenceI[] s  = null;
    
    try {
      
      // Parse the command line options
      //
      // -seqfile <seqfile> -gfffile <gfffile>
      
      Hashtable opts = GetOptions.get(args);
      
      String seqfile   = null;
      String gfffile   = null;
      boolean noseq    = true;

      if (opts.containsKey("-seqfile")) {
	seqfile  = (String)opts.get("-seqfile");
      }
      if (opts.containsKey("-gfffile")) {
	gfffile  = (String)opts.get("-gfffile");
      }        


      if (seqfile != null) {
	GappedFastaFile f = new GappedFastaFile(seqfile,"File");
	s  = f.getSeqsAsArray();
	al = new Alignment(s);
      } else {
	al  = new Alignment();
      }
	
      if (gfffile != null) {
	gff   = new GFFFile(gfffile,"File");
	
	Hashtable   fhash  = gff.getFeatureHash();
	Enumeration en     = fhash.keys();
          
	while (en.hasMoreElements()) {
	    String type = (String)en.nextElement();
	    GFF g = (GFF)fhash.get(type);
	    Vector bumpgff = pogvue.io.GFFFile.bumpGFF(g);

	    for (int i = 0 ; i < bumpgff.size(); i++) {
	      GFF tmpgff = (GFF)bumpgff.elementAt(i);
	      al.addSequence(tmpgff);	    
	    }
	}
      }
      if (al != null) {
	TestViewport tv = new TestViewport(al);
      }
      
    } catch (IOException e) {
      System.out.println("IOException " + e);
    }
  }
}
    